package com.example.day1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.day1.common.CustomException;
import com.example.day1.entity.Category;
import com.example.day1.entity.Dish;
import com.example.day1.entity.Setmeal;
import com.example.day1.mapper.CategoryMapper;
import com.example.day1.service.CategoryService;
import com.example.day1.service.DishService;
import com.example.day1.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
//    自定义方法，解决对菜品删除时的关联问题
    public void remove(Long id) throws CustomException {
        LambdaQueryWrapper<Dish> LambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper1.eq(Dish::getId,id);
        int count = dishService.count(LambdaQueryWrapper1);
        if(count>0){
            throw new CustomException("删除分类关联了菜品，不嫩删除");
        }
        LambdaQueryWrapper<Setmeal> LambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper2.eq(Setmeal::getId,id);
        int count2=setmealService.count(LambdaQueryWrapper2);
        if(count2>0){
            throw new CustomException("删除分类关联了套餐，不能删除");
        }
        //最终删除
        super.removeById(id);
    }
}
